<?php

namespace app\common\library;


use fast\Http;
use fast\Random;
use think\Cache;

/**
 * php-demo，仅供参考
 */
class Openapi
{
	// URI前缀
	protected static $base_uri = "https://openapi.fengqiyunsaas.com.cn";
	
	// key
	protected static $app_id = "ak1788752770576728064";
	
	// 秘钥
	protected static $app_secret = "C1fjXIiq27vrR3mqXp7BP";
	protected static $aes_key = "1AECC27AB3569C0E";
	protected static $aes_iv = "50B7CB67EB975D90";
	protected static $des_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoS01weWebyjWJUPwZi7/ZscEhEc1+nya0qQIgRAXhcs1jzmad8Nrus6y8jawBIDq9hpJoqq1f6DAJuLIwHGH66OMAqw97Hhnl4bcfRuK1O7D9UzlW2fSAds12Bv1H9Vr3kMjfUcNtRnUWrlqKZ0bdXp36S7ejOzdkCG+ZCHwYsWocjF+mPztBoS2aSmp0lYN09jnqS4pYv0tlGV3Sr7bYXWqherVwCkfiJe4xl1+0S5JtCmjbGzX08TKPUcRP/n3je6bHT30OWw/ErOX61tD5Hcr6q4AhRTqVZECV/ikq6Nbd2J+Pt7d5edSt8g5o6YK+e9vBisnH4+LQGlOq9hvZwIDAQAB";
	
	
	/**
	 * POST请求接口方法
	 * @param string $method
	 * @param array $params
	 * @return string
	 * @throws \Exception
	 */
	public static function requestPost(string $method, array $params = []): string
	{
		$postUrl = self::$base_uri . $method;
		// 获取当前时间的秒级时间戳
		$timestamp = time();
		// 获取当前时间的毫秒部分
		$milliseconds = substr((string)microtime(true), -3);
		// 拼接秒级时间戳和毫秒部分得到毫秒级时间戳
		$milliseconds_timestamp = $timestamp . $milliseconds;
		
		$params['appId'] = self::$app_id;
		$params['nonce'] = Random::uuid();
		$params['timestamp'] = $milliseconds_timestamp;
		//ASCII码从小到大排序
		ksort($params);
		
		// 将排序后的键值对转换为 URL 格式  然后在后面拼接 app_secret
		$stringSignTemp = urldecode(http_build_query($params) .'&appSecret='. self::$app_secret);

		//MD5加密并且转大写
		$signValue = strtoupper(md5($stringSignTemp));
		//拼接签名sign参数
		$params['sign'] = $signValue;
		
		//全部参数转json
		$json = json_encode($params);
		
		$curlPost = [
			'appId' => self::$app_id,
			'data' => strtoupper(self::desEncrypt($json))
		];
		return Http::post($postUrl, $curlPost);
	}
	
	// 加密函数
	public static function aesEncrypt($data)
	{
		$cipher = "aes-128-cfb";
		// 手动添加PKCS#5填充
		$blocksize = 16; // AES块大小为16字节
		$padding = $blocksize - (strlen($data) % $blocksize);
		$data .= str_repeat(chr($padding), $padding);
		
		$encrypted = openssl_encrypt($data, $cipher, self::$aes_key, OPENSSL_RAW_DATA, self::$aes_iv);
		return base64_encode($encrypted);
	}
	
	// 解密函数
	public static function aesDecrypt($data)
	{
		$data = base64_decode($data);
		$cipher = "aes-128-cfb";
		// 手动添加PKCS#5填充
		$blocksize = 16; // AES块大小为16字节
		$padding = $blocksize - (strlen($data) % $blocksize);
		$data .= str_repeat(chr($padding), $padding);
		
		$decrypted = openssl_decrypt($data, $cipher, self::$aes_key, OPENSSL_RAW_DATA,  self::$aes_iv);
		return $decrypted;
	}
	
	public static function desEncrypt($data)
	{
		$iv = '00000000';
		// 使用DES/CBC/PKCS5Padding加密
		$encrypted = openssl_encrypt($data, 'des-cbc', self::$des_key, OPENSSL_RAW_DATA,$iv);
		return bin2hex($encrypted);
	}
	
	public static function desDecrypt($data)
	{
		$iv = '00000000';
		$data = hex2bin($data);
		
		// 解密
		$decrypted = openssl_decrypt($data, 'des-cbc', self::$des_key, OPENSSL_RAW_DATA, $iv);
		return $decrypted;
	}
	
}

